const ver = "5";
const time = 1; // min

if ("serviceWorker" in navigator) {
  navigator.serviceWorker.register("./sw.js?v=" + ver).then(swReg => {
    console.log("swReg===reg！", swReg);


    // const timer = setInterval(() => {
    //   console.log('----')
    //   swReg.update().catch(e => {});
    // }, 1000 * 60 * time);



    swReg.addEventListener("updatefound", () => {
      const installing = swReg.installing;

      installing.addEventListener("statechange", () => {
        if (installing.state === "installed") {
          if (navigator.serviceWorker.controller) {
            alert("sw已发生变化，马上更新~。 ver: "+ ver + ', time: ' + time );
            location.reload();
          }
        }
      });
    });
  });

  navigator.serviceWorker.addEventListener("message", e => {
    console.log(e);
    alert(e.data);
  });
}
